package com.lam.datasave.diary;

import java.util.Calendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DiaryDBAdapter {
	
	public static final String DATABASE_NAME="diary.db";
	public static final String DATABASE_TABLE="diary";
	public static final int DATABASE_VERSION=3;
	public static final String KEY_ROWID="_id";
	public static final String KEY_TITLE="title";
	public static final String KEY_CREATED="createdTime"; 
	public static final String KEY_CONTENT="content";
	
	private DatabaseHelper dbHelper;
	
	public DiaryDBAdapter(Context context){
		dbHelper=new DatabaseHelper(context);
	}
	
	
	
	public DatabaseHelper getDbHelper() {
		return dbHelper;
	}





	public static class DatabaseHelper extends SQLiteOpenHelper{
		
		private static final String DATABASE_CREATE="CREATE TABLE "+DATABASE_TABLE+"(" +
				KEY_ROWID+ " INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL,"+
				KEY_TITLE + " TEXT,"+
				KEY_CONTENT +" TEXT,"+
				KEY_CREATED +" TEXT"+
				");";
		
		
		
		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context,DATABASE_NAME,null,DATABASE_VERSION);
			
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			Log.i("add","create"+DATABASE_CREATE);
			db.execSQL(DATABASE_CREATE);
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
			onCreate(db);
		}
		
		public SQLiteDatabase open(){
				return getWritableDatabase();
		}
		
		public Cursor getAllNotes(){
			
			return getWritableDatabase().query(DATABASE_TABLE, null, null, null, null, null, null);
		}
		
//		public void close(){
//			if(diaryDB!=null&&diaryDB.isOpen()){
//				diaryDB.close();
//			}
//		}
		
		public long createDairy(String title, String content){
			
			ContentValues contentValues=new ContentValues();
			contentValues=initContentValues(contentValues, title, content);
			return getWritableDatabase().insert(DATABASE_TABLE, null,contentValues);
		}
		
		public boolean updateDiary(long rowId,String title,String content){
			ContentValues contentValues=new ContentValues();
			contentValues=initContentValues(contentValues, title, content);
			return getWritableDatabase().update(DATABASE_TABLE, contentValues,KEY_ROWID+"="+rowId,null)>0;
			
		}
		
		public void deleteDiaryById(Long id){
			getWritableDatabase().delete(DATABASE_TABLE, KEY_ROWID+"="+id, null);
		}
		
		private ContentValues initContentValues(ContentValues c,String title, String content){
			ContentValues contentValues = new ContentValues();
			contentValues.put(KEY_TITLE, title);
			contentValues.put(KEY_CONTENT,content );
			Calendar calendar = Calendar.getInstance();
			String created = calendar.get(Calendar.YEAR)+"/"+
					calendar.get(Calendar.MONTH)+"/"+
					calendar.get(Calendar.DAY_OF_MONTH)+"/"+
					calendar.get(Calendar.HOUR_OF_DAY)+"/"+
					calendar.get(Calendar.MINUTE);
			contentValues.put(KEY_CREATED, created);
			return contentValues;
		}
		
	}

}
